package com.mediacloud.app.newsmodule.view;

import android.content.Context;
import android.content.Intent;
import android.os.Build;

import androidx.annotation.RequiresApi;

import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.eventTrack.TXReportManager;
import com.mediacloud.app.model.component.ComponentItem;
import com.mediacloud.app.model.interfaces.DataInvokeCallBack;
import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.model.news.ArticleListData;
import com.mediacloud.app.newsmodule.adaptor.component.XAbsComponentHolder;
import com.mediacloud.app.user.model.UserInfo;
import com.mediacloud.app.newsmodule.activity.RelatedListActivity;
import com.mediacloud.app.newsmodule.fragment.imagetext.RelatedType;
import com.mediacloud.app.newsmodule.utils.ComponentRelativeInvoker;

import java.util.List;

/**
 * Created by 陈凯 on 2017/9/27.
 */

public abstract class BaseRelatedView extends FrameLayout {
    public Context mContext;

    protected View mView;
    public String componentId;
    public String article_id;

    private RelateArticledDataCallBack relateArticledDataCallBack;
    private ComponentRelativeInvoker componentRelativeInvoker;
    List<ArticleItem> listArticles;
    ArticleItem articleItem;

    protected XAbsComponentHolder header;
    public int type = 1;

    public BaseRelatedView(Context context) {
        super(context);
        mContext = context;
        initView();


    }

    ComponentItem componentItem;

    public void setData(ArticleItem articleItem,String article_id, ComponentItem component, int type) {
        setStyles(component);
        this.articleItem = articleItem;
        this.article_id = article_id;
        this.componentId = component.getId() + "";
        componentItem = component;
        this.type = type;
        getRelateNewsList();
    }

    public void setData(ArticleItem data, ComponentItem component) {
        setStyles(component);
        componentItem = component;
        this.componentId = component.getId() + "";
        articleItem = data;
        setBuildData();

    }

    public void setBuildData() {

    }

    protected void setStyles(ComponentItem componentItem) {
        header.setComponent(componentItem);
        setExpandStyle();
    }

    public void setExpandStyle() {

    }


    public BaseRelatedView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public BaseRelatedView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public BaseRelatedView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    public void initView() {

        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        setLayoutParams(param);
        mView = LayoutInflater.from(mContext).inflate(getViewLayOutId(), null);
        header = new XAbsComponentHolder(mView) {
            @Override
            public boolean isDetail() {
                return true;
            }

            @Override
            public void onClick(View v) {
                onMoreClick(v);
                TXReportManager.getInstance().app_jhapp_content_detail_rec_interact("点击更多",articleItem);
                Intent intent = new Intent();
                intent.putExtra("data", componentItem);
                intent.setClass(getContext(), RelatedListActivity.class);
                intent.putExtra("componentId", componentId);
                intent.putExtra("article_id", article_id);
                intent.putExtra("type", type);
                getContext().startActivity(intent);
            }
        };

        addView(mView);
        this.setVisibility(GONE);
    }

    abstract int getViewLayOutId();

    public void getRelateNewsList() {
        relateArticledDataCallBack = new RelateArticledDataCallBack();
        componentRelativeInvoker = new ComponentRelativeInvoker(relateArticledDataCallBack);
        if (UserInfo.isLogin(getContext()) && componentItem != null && (componentItem.getType() == RelatedType.HOT_REVIEWS || componentItem.getType() == RelatedType.LASTEST_REVIEW))
            componentRelativeInvoker.getContentComponent(null,componentId, article_id, UserInfo.getUserInfo(getContext()).getUserid(), null, type, 0, 0);
        else
            componentRelativeInvoker.getContentComponent(null,componentId, article_id, null, null, type, 0, 0);
    }

    class RelateArticledDataCallBack implements DataInvokeCallBack<ArticleListData> {


        @Override
        public void success(ArticleListData data) {

            if (data != null) {
                listArticles = data.articleList;

                if (listArticles == null || listArticles.size() == 0) {
                    //  setVisibility(View.GONE);
                    return;
                }

                setVisibility(VISIBLE);
                onNetDataSuccess(data);
            }
        }

        @Override
        public void fault(Object data) {
            /*   mRootView.setVisibility(View.GONE);*/
            onNetDataFailure(data);
        }
    }

    abstract void onNetDataSuccess(ArticleListData data);

    abstract void onNetDataFailure(Object data);
}